//
//  ElectronDetailViewController.m
//  阿帕
//
//  Created 阿帕 on 2020/1/4.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "ElectronDetailViewController.h"
#import <QuickLook/QuickLook.h>

@interface ElectronDetailViewController ()<QLPreviewControllerDataSource, QLPreviewControllerDelegate>

@property (strong, nonatomic)QLPreviewController *previewController;

@property (copy, nonatomic)NSURL *fileURL;

@end

@implementation ElectronDetailViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationItem.title = _pageTitle;
    
    self.previewController  =  [[QLPreviewController alloc] init];
    //    self.previewController.view.frame = CGRectMake(0, NavHeight, ScreenW, ScreenH-NavHeight);
    self.previewController.dataSource  = self;
    
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    NSString *urlStr = _contractPath;
    NSString *fileName = [urlStr lastPathComponent]; //获取文件名称
    NSURL *URL = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    //判断是否存在
    if([self isFileExist:fileName]) {
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        NSURL *url = [documentsDirectoryURL URLByAppendingPathComponent:fileName];
        self.fileURL = url;
        //        self.previewController.navigationController.navigationBar.userInteractionEnabled = YES;
        //        self.previewController.view.userInteractionEnabled = YES;
        //        [self.view addSubview:self.previewController.view];
        [self addChildViewController:self.previewController];
        self.previewController.view.frame = self.view.bounds;
        [self.view addSubview:self.previewController.view];
        
        //        [self presentViewController:self.previewController animated:YES completion:nil];
    }else {
        [SVProgressHUD showWithStatus:@"下载中"];
        NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress){
            
        } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
            NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
            NSURL *url = [documentsDirectoryURL URLByAppendingPathComponent:fileName];
            return url;
        } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
            [SVProgressHUD dismiss];
            self.fileURL = filePath;
            //            self.previewController.navigationController.navigationBar.userInteractionEnabled = YES;
            //            self.previewController.view.userInteractionEnabled = YES;
            //            [self.view addSubview:self.previewController.view];
            [self addChildViewController:self.previewController];
            self.previewController.view.frame = self.view.bounds;
            [self.view addSubview:self.previewController.view];
            //            [self presentViewController:self.previewController animated:YES completion:nil];
        }];
        [downloadTask resume];
    }
}

///这个方法是用于返回阅读的文档数量
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}

///这个方法是用于返回文档的路径或文档的名字，这样，就不得不说一下QLPreviewItem，这个是一个继承于NSObject的类，它没有创建的方法，但是默认它为NSURL或者NSString的类型，但是要强调一点，这里的NSURL为文档的本地路径，而不是网络路径。
- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
    return self.fileURL;
}

//判断文件是否已经在沙盒中存在
-(BOOL) isFileExist:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *filePath = [path stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL result = [fileManager fileExistsAtPath:filePath];
    return result;
}

@end
